<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="选择产品配件"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <goodsFittingsInventory
      ref="goodsFittingsInventory"
    ></goodsFittingsInventory>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
    
    <script>
import VolBox from "@/components/basic/VolBox.vue";
import goodsFittingsInventory from "@/views/produceinfo/goods_fittings_inventory/q_goods_fittings_inventory.vue";
export default {
  components: {
    "vol-box": VolBox,
    goodsFittingsInventory,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
      // this.$refs.goodsFittingsInventory.searchFormFields.AuditStatus = "1";
      // this.$refs.goodsFittingsInventory.searchFormFields.PaymentState = "0";
      // this.Staple_goods_sub = null;
      this.$refs.goodsFittingsInventory.searchFormFields.产品编码 = null;
      this.$refs.goodsFittingsInventory.searchFormFields.产品名称 = null;
      this.$refs.goodsFittingsInventory.searchFormFields.配件库位 = null;
      this.$refs.goodsFittingsInventory.searchFormFields.配件编码 = null;
      this.$refs.goodsFittingsInventory.searchFormFields.配件名称 = null;
      this.$refs.goodsFittingsInventory.searchFormFields.可用数量 = [
        null,
        null,
      ];
    },
    open(Goods_sub,WHSE_scgw,WHSE_jg) {
      this.WHSE_scgw=WHSE_scgw
      this.model = true;
      try {
        // this.Staple_goods_sub = Goods_sub;
        let setIntervalTiem = setInterval(() => {
          if (this.$refs.goodsFittingsInventory) {
            clearInterval(setIntervalTiem);
            this.$refs.goodsFittingsInventory.searchFormFields.可用数量 = [
              1,
              null,
            ];
            this.$refs.goodsFittingsInventory.searchFormFields.产品编码 =
              Goods_sub;
            this.$refs.goodsFittingsInventory.searchFormFields.配件库位 = WHSE_jg
            this.$refs.goodsFittingsInventory.$refs.grid.search();
          }
        }, 100);
      } catch (error) {
        console.error(error);
      }
    },
    async detailOnSelect() {
      let rowObj = this.$refs.goodsFittingsInventory.$refs.grid.getSelectRows();
      if (rowObj.length) {
        // let newRowObj = []
        this.$emit("parentCall", ($parent) => {
          let detailRowData = $parent.$refs.detail.rowData;
          // detailRowData.forEach((item) => {
          //   rowObj.forEach((o) => {
          //     if (
          //       o.Goods_sub != item.Goods_sub &&
          //       o.WHSE_name != item.WHSE_name
          //     ){
          //       newRowObj.push(item)
          //     }
          //   });
          // });
          let newRowObj = [];
          newRowObj = rowObj.map((item) => {
            return {
              goodsInventoryWarehouseXm: item.ID,
              // Staple_goods_sub: item.产品编码,
              // Staple_goods_name: item.产品名称,
              Goods_sub: item.配件编码,
              Goods_name: item.配件名称,
              Goods_num: item.所需数量,
              // Goods_num_copy: item.所需数量,
              Goods_size: item.规格,
              WHSE_name: item.配件库位,
              // Production_state: 1,
              AvailableQuantity: item.可用数量,
              Cost_price: item.成本价,
              Type: 1,
              WHSE_no:this.WHSE_scgw,
              Tax_exclusive: item.无税成本,
              Cost_accounting:item.成本核算,
            };
          });
          let arr = [];
          let stringName = "";
          detailRowData.forEach((item) => {
            newRowObj.forEach((j) => {
              if (
                j.goodsInventoryWarehouseXm == item.goodsInventoryWarehouseXm
              ) {
                stringName += stringName ? "," + j.Goods_name : j.Goods_name;
                arr.push(j.goodsInventoryWarehouseXm);
              }
            });
          });
          if (stringName != "") {
            this.base.ElMessageFn("明细表已存在 " + stringName) 
          }
          arr.forEach((item) => {
            let index = newRowObj.findIndex(
              (x) => x.goodsInventoryWarehouseXm == item
            );
            if (index != -1) {
              newRowObj.splice(index, 1);
            }
          });
          $parent.$refs.detail.rowData.unshift(...newRowObj);
        });

        this.onModelClose();
        return (this.model = false);
      } else {
        this.base.ElMessageFn("请至少选择一件商品", "warning");

      }
    },
  },
};
</script>
    
    <style>
</style>